//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FdmSnapshotCondition.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmSnapshotCondition::CFdmSnapshotCondition (Double t) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phFdmSnapshotCondition = NULL;
#endif
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        _ppFdmSnapshotCondition = new boost::shared_ptr<QuantLib::FdmSnapshotCondition> (new QuantLib::FdmSnapshotCondition ( _t ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmSnapshotCondition::CFdmSnapshotCondition (boost::shared_ptr<QuantLib::FdmSnapshotCondition>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSnapshotCondition = NULL;
#endif
	_ppFdmSnapshotCondition = &childNative;
    
}
Cephei::QL::Experimental::Finitedifferences::CFdmSnapshotCondition::CFdmSnapshotCondition (QuantLib::FdmSnapshotCondition& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmSnapshotCondition = NULL;
#endif
	_ppFdmSnapshotCondition = new boost::shared_ptr<QuantLib::FdmSnapshotCondition> (&childNative);
    
    _FdmSnapshotConditionOwner = owner;
    
}

Cephei::QL::Experimental::Finitedifferences::CFdmSnapshotCondition::CFdmSnapshotCondition (CFdmSnapshotCondition^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSnapshotCondition = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmSnapshotCondition = new boost::shared_ptr<QuantLib::FdmSnapshotCondition> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmSnapshotCondition::CFdmSnapshotCondition (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSnapshotCondition = NULL;
#endif
	if (!t->IsSubclassOf(CFdmSnapshotCondition::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdmSnapshotCondition::CFdmSnapshotCondition (QuantLib::Handle<QuantLib::FdmSnapshotCondition>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmSnapshotCondition = &childNative;
	_ppFdmSnapshotCondition = &static_cast<boost::shared_ptr<QuantLib::FdmSnapshotCondition>>(childNative.currentLink());
    
    _FdmSnapshotConditionOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdmSnapshotCondition::CFdmSnapshotCondition (QuantLib::Handle<QuantLib::FdmSnapshotCondition> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmSnapshotCondition = &childNative;
	_ppFdmSnapshotCondition = &static_cast<boost::shared_ptr<QuantLib::FdmSnapshotCondition>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmSnapshotCondition::CFdmSnapshotCondition (QuantLib::FdmSnapshotCondition childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSnapshotCondition = NULL;
#endif
	_ppFdmSnapshotCondition = new boost::shared_ptr<QuantLib::FdmSnapshotCondition> (new QuantLib::FdmSnapshotCondition (childNative));
    
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdmSnapshotCondition::~CFdmSnapshotCondition ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmSnapshotCondition != NULL)
    {
	    delete _ppFdmSnapshotCondition;
        _ppFdmSnapshotCondition = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmSnapshotCondition::!CFdmSnapshotCondition ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmSnapshotCondition != NULL)
    {
	    delete _ppFdmSnapshotCondition;
    }
}
QuantLib::FdmSnapshotCondition& Cephei::QL::Experimental::Finitedifferences::CFdmSnapshotCondition::GetReference ()
{
    if (_ppFdmSnapshotCondition == NULL) throw gcnew NativeNullException ();
	return **_ppFdmSnapshotCondition;
}
boost::shared_ptr<QuantLib::FdmSnapshotCondition>& Cephei::QL::Experimental::Finitedifferences::CFdmSnapshotCondition::GetShared ()
{
    if (_ppFdmSnapshotCondition == NULL) throw gcnew NativeNullException ();
	return *_ppFdmSnapshotCondition;
}
QuantLib::FdmSnapshotCondition* Cephei::QL::Experimental::Finitedifferences::CFdmSnapshotCondition::GetPointer ()
{
    if (_ppFdmSnapshotCondition == NULL) throw gcnew NativeNullException ();
	return &**_ppFdmSnapshotCondition;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmSnapshotCondition>& Cephei::QL::Experimental::Finitedifferences::CFdmSnapshotCondition::GetHandle ()
{
	if (_phFdmSnapshotCondition == NULL)
	{
		_phFdmSnapshotCondition = new Handle<QuantLib::FdmSnapshotCondition> (*_ppFdmSnapshotCondition);
	}
	return *_phFdmSnapshotCondition;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdmSnapshotCondition::HasNative () 
{
	return (_ppFdmSnapshotCondition != NULL);
}

Cephei::QL::Experimental::Finitedifferences::IFdmSnapshotCondition^ Cephei::QL::Experimental::Finitedifferences::CFdmSnapshotCondition::ApplyTo (Cephei::QL::Math::IArray^ a, Double t)
{
    CArray^ _Ca;
    try
    {
        _Ca = safe_cast<CArray^> (a);
        _Ca->Lock();
        QuantLib::Array& _a = static_cast<QuantLib::Array&> (_Ca->GetReference ()); 
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
    	(*_ppFdmSnapshotCondition)->applyTo ( _a,  _t );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ca != nullptr) _Ca->Unlock();
    }
}
Double Cephei::QL::Experimental::Finitedifferences::CFdmSnapshotCondition::GetTime::get ()
{
    try
    {
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppFdmSnapshotCondition)->getTime ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Experimental::Finitedifferences::CFdmSnapshotCondition::GetValues::get ()
{
    try
    {
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppFdmSnapshotCondition)->getValues ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Finitedifferences::IFdmSnapshotCondition^ Cephei::QL::Experimental::Finitedifferences::CFdmSnapshotCondition_Factory::Create (Double t)
{
    return gcnew CFdmSnapshotCondition ( t);
}
